﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using blackhouse.WebNemo.Business;
using blackhouse.WebNemo.Business.Groups;
using blackhouse.WebNemo.Business.Translate;
using System.Globalization;

namespace blackhouse.WebNemo.Web.Controls {
    public partial class EditTranslate : System.Web.UI.UserControl {

        #region Fields

        private int groupId;
        private int fieldId;
        private int recordId;

        private Factory factory = Factory.Instance();

        private GroupColumn column;
        private GroupRecord record;
        private EditController controller;

        private ITranslate translate;

        #endregion

        #region Start Point

        protected override void OnInit(EventArgs e) {
            base.OnInit(e);

            this.ReadParameters();
        }

        protected override void OnLoad(EventArgs e) {
            base.OnLoad(e);

            this.SetInstance();
            this.PopulateControls();
        }

        #endregion

        #region Methods

        private void ReadParameters() {
            Int32.TryParse(Request["groupId"], out groupId);
            Int32.TryParse(Request["fieldId"], out fieldId);
            Int32.TryParse(Request["recordId"], out recordId);
        }

        private void SetInstance() {
            this.record = new GroupRecord(groupId, recordId);
            if (record.Exists) {
                this.column = this.record.GetGroup().Columns[fieldId];
                this.controller = new EditController(column, record);

                this.translate = this.factory.GetTranslates().GetTranslate(this.groupId, this.fieldId, this.recordId);
            }
        }

        private void PopulateControls() {
            this.controller.Populate(this.lblName,
                null,
                    this.lblDescription,
                    this.pnlEdit,
                    this.pnlValidators,
                    this.pnlHolder,
                    null,
                    this.pnlRecordDisplay);

            IEnumerable<CultureInfo> cultures = this.translate.GetAvalibleLanguages().OrderBy(c => c.DisplayName);
            this.ddlLanguages.DataSource = cultures;
            this.ddlLanguages.DataBind();
            this.ddlLanguages2.DataSource = cultures;
            this.ddlLanguages2.DataBind();

            CultureInfo ci1 = GetCurrentCultureInfo(), ci2 = GetCurrentReferenceCultureInfo();

            this.ddlLanguages.SelectedValue = ci1.Name;
            this.ddlLanguages2.SelectedValue = ci2.Name;

            this.ltlOryginalText.Text = this.translate.TranslationOryginal();

            if (ci2.Name == "pl-PL")
                this.ltlRefText.Text = this.ltlOryginalText.Text;
            else
                this.ltlRefText.Text = this.translate.Translation(ci2);

            if (string.IsNullOrEmpty(this.ltlRefText.Text))
                this.ltlRefText.Text = "Brak tekstu dla wybranego języka!";

            this.controller.ReSetValue(this.translate.Translation(ci1));

            this.lnkBaseIdentyficator.Text = this.record.Identyficator;
            this.lnkBaseIdentyficator.ToolTip = this.record.Identyficator;
        }

        private CultureInfo GetCurrentCultureInfo() {
            string ecl;
            HttpCookie hc = Request.Cookies["edit-culture"];
            if (hc != null) {
                ecl = hc.Value;
            }
            else
                ecl = "en-GB";

            try {
                return CultureInfo.GetCultureInfo(ecl);
            }
            catch (Exception) {
                return CultureInfo.GetCultureInfo("en-GB");
            }

        }

        private CultureInfo GetCurrentReferenceCultureInfo() {
            string ecl;
            HttpCookie hc = Request.Cookies["ref-culture"];
            if (hc != null) {
                ecl = hc.Value;
            }
            else
                ecl = "pl-PL";

            try {
                return CultureInfo.GetCultureInfo(ecl);
            }
            catch (Exception) {
                return CultureInfo.GetCultureInfo("pl-PL");
            }
        }

        protected void Save(object sender, EventArgs e) {
            string value = this.controller.GetValue();
            this.translate.SetTranslation(GetCurrentCultureInfo(), value);
            this.record.Save();
            this.pnlAutoCloseScript.Visible = true;
        }

        #endregion

        protected void ddlLanguages_SelectedIndexChanged(object sender, EventArgs e) {
            Response.Cookies.Add(new HttpCookie("edit-culture", Request[ddlLanguages.UniqueID]));
            Response.Redirect(Request.RawUrl);
        }

        protected void ddlLanguages2_SelectedIndexChanged(object sender, EventArgs e) {
            Response.Cookies.Add(new HttpCookie("ref-culture", Request[ddlLanguages2.UniqueID]));
            Response.Redirect(Request.RawUrl);
        }

    }
}